package daoServiceImpl;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import model.Myboard;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import daoService.MyboardDAO;

public class MyboardDAOImpl implements MyboardDAO{
public static void main(String[] args){
	new MyboardDAOImpl();
}	
// Board.xml <select resultMap="BoardResult"> 으로 수정
private static SqlMapClient sqlMapper;
static {
	try {
		Reader reader = Resources.getResourceAsReader(
				"sql_map_config.xml"); 
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(
				reader);
		System.out.println("sqlMapper :" + sqlMapper);
		reader.close();
	} catch (IOException e) {
		throw new RuntimeException(
				"Something bad happened while building the SqlMapClient instance."
						+ e, e);
	}
}; 	

	@Override
	public int insert(Myboard myboard) {
		try {
			// param 1 <insert 의 id 값> 
			// parma 2 <insert의 parameterClass 값>  
			 sqlMapper.insert(
					"insertMyboard",myboard);
		} catch (Exception e) {
			e.printStackTrace(); 
		}
		return -1;
	}

	@Override
	public List<Myboard> selectAll() {
		try {
			// param 1 <select 의 id 값> 
			return sqlMapper.queryForList("selectAll");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Myboard select(int num) {  
		try {
			// Myboard.xml 안의 <select id="selectByNum" 호출>
			return (Myboard) sqlMapper.queryForObject(
					"selectByNum", num);
		} catch (SQLException e) {
			e.printStackTrace();
		}return null;
	}

	@Override
	public int delete(int num) {
		try {
			return sqlMapper.delete("deleteByNum", num);
		} catch (SQLException e) {		
			e.printStackTrace();
		}
		return -1; 
	}

	@Override
	public int update(Myboard myboard) {
		try {
			return sqlMapper.update("update", myboard);
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return -1;
	}

}
